<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var nums = [12,3,45,6,4,98,15,42];
        //外层循环控制比较的轮数，每一轮确定一个数字。
        for (var i = 0;i<nums.length-1;i++){
            //内层循环控制每轮的比较次数
            for(var j = 0;j<nums.length-i-1;j++){
               //升序
               if(nums[j]>nums[j+1]){
                var index = nums[j];
                nums[j]=nums[j+1];
                nums[j+1]=index;
               }
                
            } 
        }
        console.log(nums);
    </script>
</body>
</html>